/*
 * Copyright (C) 2022 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package cn.kcrx.autoweightapp.fragment

import android.app.DatePickerDialog
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import cn.kcrx.autoweightapp.core.BaseFragment
import cn.kcrx.autoweightapp.databinding.FragmentQueryBinding
import cn.kcrx.autoweightapp.fragment.adapter.CashWeightRecordAdapter
import cn.kcrx.autoweightapp.fragment.viewmodes.FragmentQueryViewModel
import cn.kcrx.autoweightapp.utils.XToastUtils
import com.xuexiang.xpage.annotation.Page
import com.xuexiang.xpage.enums.CoreAnim
import dagger.hilt.android.AndroidEntryPoint
import mu.KotlinLogging
import java.util.*


@AndroidEntryPoint
@Page(name="称重记录查询结果",anim=CoreAnim.slide)
class FramgengQuery:BaseFragment<FragmentQueryBinding>() {
    val logger=KotlinLogging.logger{this.javaClass.simpleName}
    private val viewmodel: FragmentQueryViewModel by viewModels()
    private val adapter= CashWeightRecordAdapter()
    override fun viewBindingInflate(
        inflater: LayoutInflater,
        container: ViewGroup
    ): FragmentQueryBinding {
      return FragmentQueryBinding.inflate(inflater,container,false)
    }
    override fun initViews() {
        logger.error { "context type is ${requireContext().javaClass}" }
        binding!!.rvQueryMain.layoutManager= LinearLayoutManager(context)
        binding!!.rvQueryMain.adapter=adapter
        binding!!.rvQueryMain.itemAnimator= DefaultItemAnimator()
        viewmodel.record.observe(viewLifecycleOwner){
            logger.error { "查询到所有记录 size=${it.size}" }
            adapter.submitList(it)
        }
         val fromDate= arguments?.getLong("fromDate")
         val toDate=arguments?.getLong("toDate")
        refreshRV(fromDate,toDate)
    }

    private fun refreshRV(fromDate:Long?,toDate:Long?) {
        if(fromDate!=null&&toDate!=null){
            viewmodel.getTodayDate(Date(fromDate),Date(toDate))
        }else{
         logger.error { "起止时间为空，无法查询" }
        }
    }

    override fun initListeners() {
        super.initListeners()
        binding!!.btnExport.setOnClickListener {
            try{
                viewmodel.exportExcel(requireContext())
                XToastUtils.success("excel数据导出成功")
            }catch (e:Exception){
                 logger.error(e) { "excel导出失败!"}
                 XToastUtils.error("excel导出失败!")
            }

        }
    }
}